package com.ermao.mymes.support.pipeline;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.ermao.mymes.common.enums.RespStatusEnum;
import com.ermao.mymes.common.vo.BasicResultVO;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;

/**
 * 流程控制器
 *
 * @author Ermao
 * Date: 2022/5/7 19:16
 */
@Slf4j
@Data
public class ProcessController {
	/**
	 * 模板映射，根据 Code 获取相应的责任链
	 */
	private Map<String, ProcessTemplate> templateConfig = null;


	/**
	 * 执行责任链
	 *
	 * @param context
	 * @return 返回上下文内容
	 */
	public ProcessContext process(ProcessContext context) {

		/**
		 * 前置检查
		 */
		if (!preCheck(context)) {
			return context;
		}


		// 遍历某个流程节点,出现异常往外抛，遇到终止就结束
		List<BusinessProcess> processList = templateConfig.get(context.getCode()).getProcessList();
		for (BusinessProcess businessProcess : processList) {
			businessProcess.process(context);
			if (context.getNeedBreak()) {
				break;
			}
		}
		return context;
	}

	private Boolean preCheck(ProcessContext context) {
		// 上下文
		if (context == null) {
			context.setResponse(BasicResultVO.fail(RespStatusEnum.CONTEXT_IS_NULL));
			return false;
		}

		// 业务代码
		String businessCode = context.getCode();
		if (StrUtil.isBlank(businessCode)) {
			context.setResponse(BasicResultVO.fail(RespStatusEnum.BUSINESS_CODE_IS_NULL));
			return false;
		}

		// 执行模板
		ProcessTemplate processTemplate = templateConfig.get(businessCode);
		if (processTemplate == null) {
			context.setResponse(BasicResultVO.fail(RespStatusEnum.PROCESS_TEMPLATE_IS_NULL));
			return false;
		}

		// 执行模板列表
		List<BusinessProcess> processList = processTemplate.getProcessList();
		if (CollUtil.isEmpty(processList)) {
			context.setResponse(BasicResultVO.fail(RespStatusEnum.PROCESS_LIST_IS_NULL));
			return false;
		}

		return true;
	}

}
